package com.example.myone.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.myone.Adapter.DiscoverArticleAdapter;
import com.example.myone.Entity.Article;
import com.example.myone.Entity.User;
import com.example.myone.Mapper.ArticleMapper;
import com.example.myone.Mapper.UserMapper;
import com.example.myone.R;
import com.example.myone.Util.ImageUtils;

import java.util.List;

public class AuthorActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private DiscoverArticleAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.author_activity);
        recyclerView = findViewById(R.id.recyclerView);
        // 返回
        ImageView topBack = findViewById(R.id.top_back_button);
        topBack.setOnClickListener(v -> finish());
        // 标题
        TextView topTitle = findViewById(R.id.top_title);
        topTitle.setText("作者");
        // 获取参数：作者的用户Id
        Intent intent = getIntent();
        int authorId = intent.getIntExtra("author_id", -1);
        if (authorId != -1) {
            User authorUser = UserMapper.getUserById(this, authorId);
            TextView username = findViewById(R.id.username);
            ImageView avatar = findViewById(R.id.avatar);
            if (authorUser != null) {
                username.setText(authorUser.username);
                ImageUtils.setImageByBase64Str(this, avatar, authorUser.avatar);
            }
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            List<Article> initialData = ArticleMapper.getArticleByAuthorId(this, authorId);
            adapter = new DiscoverArticleAdapter(this, initialData);
            recyclerView.setAdapter(adapter);
        }

    }
}
